package org.jeecg.modules.solitaire_app.app.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.api.client.util.ArrayMap;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.YsyHotnews;
import org.jeecg.modules.solitaire_app.app.entity.YsyMarketnews;
import org.jeecg.modules.solitaire_app.app.entity.YsyProductionMarketing;
import org.jeecg.modules.solitaire_app.app.mapper.YsyHotnewsMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyMarketnewsMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyProductionMarketingMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyProductionMarketingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description: 产销比数据
 * @Author: jeecg-boot
 * @Date:   2024-04-07
 * @Version: V1.0
 */
@Service
@DS("ysy_db")
public class YsyProductionMarketingServiceImpl extends ServiceImpl<YsyProductionMarketingMapper, YsyProductionMarketing> implements IYsyProductionMarketingService {

    @Autowired
    private YsyMarketnewsMapper ysyMarketnewsMapper;

    @Autowired
    private YsyHotnewsMapper ysyHotnewsMapper;

    @Override
    public Result<JSONArray> getYsyProductionMarketing() {
        Result<JSONArray> result = new Result<>();
        try {
            //汽油
            List<YsyProductionMarketing> ysyProductionMarketings = this.baseMapper.getYsyProductionMarketing(1,7);
            Collections.reverse(ysyProductionMarketings);
            //柴油
            List<YsyProductionMarketing> ysyProductionMarketings1 = this.baseMapper.getYsyProductionMarketing(2,7);
            Collections.reverse(ysyProductionMarketings1);

            JSONArray jsonArray = new JSONArray();

            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");

            //下标0-汽油
            JSONArray qyArr = new JSONArray();
            ysyProductionMarketings.stream().forEach(item->{
                JSONObject qyJson = new JSONObject();
                qyJson.putOpt("dates",sdf.format(item.getCreateTime()));
                qyJson.putOpt("price",item.getProportion());
                qyArr.add(qyJson);
            });
            jsonArray.add(qyArr);

            //下标1-柴油
            JSONArray cyArr = new JSONArray();
            ysyProductionMarketings1.stream().forEach(item->{
                JSONObject cyJson = new JSONObject();
                cyJson.putOpt("dates",sdf.format(item.getCreateTime()));
                cyJson.putOpt("price",item.getProportion());
                cyArr.add(cyJson);
            });
            jsonArray.add(cyArr);

            //下标2-行业早报
            String ysyMarketnews = ysyMarketnewsMapper.getTopOneMarketNews().getTitle();
            jsonArray.add(ysyMarketnews);

            //下标3-热点聚集
            String ysyHotNews = ysyHotnewsMapper.getTopOneHotNews().getTitle();
            jsonArray.add(ysyHotNews);


            result.setCode(200);
            result.setSuccess(true);
            result.setResult(jsonArray);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("服务器响应失败");
            return result;
        }

    }

    @Override
    public Result<Map<String,Object>> getTopNewProductionMarketing() {
        Result<Map<String,Object>> result = new Result<>();
        try {
            //汽油
            YsyProductionMarketing ysyProductionMarketings = this.baseMapper.getTopOneMarketNews(1);
            //柴油
            YsyProductionMarketing ysyProductionMarketings1 = this.baseMapper.getTopOneMarketNews(2);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Map<String,Object> map = new ArrayMap<>();

            map.put("qiyouObj",ysyProductionMarketings);
            map.put("chaiyouObj",ysyProductionMarketings1);
            map.put("currentDate",sdf.format(ysyProductionMarketings.getCreateTime()));


            result.setCode(200);
            result.setSuccess(true);
            result.setResult(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("服务器响应失败");
            return result;
        }
    }
}
